package com.huike.game.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huike.game.R;
import com.huike.game.base.Game9Table;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class GameActivity9 extends AppCompatActivity {

    private ImageView ivLeftImage;
    private TextView tvLeftText;
    private TextView tvContentTitle;
    private ImageView ivRigthImage;
    private TextView tvRightText;
    private Spinner spGame9;
    private Button btGame9Query;
    private ListView lvGame9;
    private List<Game9Table> list;
    private adapterView adapterView;
    private TableLayout tl_game9;
    private TextView tv_game9_hint;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game9);

//        模拟加载数据
        list = new ArrayList<>();
        list.add(new Game9Table("1","3","100","admin","1578634439"));
        list.add(new Game9Table("2","5","28","user","1547098439"));
        list.add(new Game9Table("3","6","8","admin","1641792839"));
        list.add(new Game9Table("4","9","30","admin","1547098439"));

//        初始化id
        initIds();

        if(list.size() <= 0) {
            tv_game9_hint.setVisibility(View.VISIBLE);
            tl_game9.setVisibility(View.GONE);
        }

//        要在初始化只会执行
        adapterView = new adapterView();
        lvGame9.setAdapter(adapterView);

        spGame9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(final AdapterView<?> adapterViews, View view, final int i, long l) {
                btGame9Query.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (i){
                            case 0://时间升序
                                Collections.sort(list, new Comparator<Game9Table>() {
                                    @Override
                                    public int compare(Game9Table game9Table, Game9Table t1) {
                                        return Integer.parseInt(game9Table.getTime()) -Integer.parseInt(t1.getTime());
                                    }
                                });
                                adapterView.notifyDataSetChanged();
                                break;
                            case 1://时间降序
                                Collections.sort(list, new Comparator<Game9Table>() {
                                    @Override
                                    public int compare(Game9Table game9Table, Game9Table t1) {
                                        return Integer.parseInt(game9Table.getTime()) -Integer.parseInt(t1.getTime());
                                    }
                                });
                                Collections.reverse(list);
                                adapterView.notifyDataSetChanged();
                                break;
                        }
                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    private void initIds() {
        ivLeftImage = (ImageView) findViewById(R.id.iv_left_image);
        tvLeftText = (TextView) findViewById(R.id.tv_left_text);
        tvContentTitle = (TextView) findViewById(R.id.tv_content_title);
        ivRigthImage = (ImageView) findViewById(R.id.iv_rigth_image);
        tvRightText = (TextView) findViewById(R.id.tv_right_text);
        spGame9 = (Spinner) findViewById(R.id.sp_game9);
        btGame9Query = (Button) findViewById(R.id.bt_game9_query);
        lvGame9 = (ListView) findViewById(R.id.lv_game9);
        tl_game9 = findViewById(R.id.tl_game9);
        tv_game9_hint = findViewById(R.id.tv_game9_hint);

        ivRigthImage.setVisibility(View.GONE);
        tvRightText.setVisibility(View.GONE);
        tvContentTitle.setText("账单管理");
        ivLeftImage.setImageResource(R.drawable.tiao);
        tvLeftText.setVisibility(View.GONE);
    }

    class adapterView extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHoder hoder = null;
            if(view == null){
                hoder = new ViewHoder();
                view = View.inflate(GameActivity9.this,R.layout.game9_table,null);
                hoder.tvGame9ID = view.findViewById(R.id.tv_game9_ID);
                hoder.tvGame9CarID = view.findViewById(R.id.tv_game9_carID);
                hoder.tvGame9Price = view.findViewById(R.id.tv_game9_price);
                hoder.tvGame9User = view.findViewById(R.id.tv_game9_user);
                hoder.tvGame9Time = view.findViewById(R.id.tv_game9_time);
                view.setTag(hoder);
            }else{
                hoder = (ViewHoder) view.getTag();
            }

            Game9Table table = list.get(i);
            Log.e("--------",table.toString() +"=---------");

            hoder.tvGame9ID.setText(table.getId());
            hoder.tvGame9CarID.setText(table.getCarid());
            hoder.tvGame9Price.setText(table.getPrice());
            hoder.tvGame9User.setText(table.getUser());
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            String time = format.format(new Date(Long.valueOf(table.getTime() + "000")));
            hoder.tvGame9Time.setText(time );


            return view;
        }
    }

    class ViewHoder{
        TextView tvGame9ID;
        TextView tvGame9CarID;
        TextView tvGame9Price;
        TextView tvGame9User;
        TextView tvGame9Time;
    }
}
